#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */

//sudo mknod /dev/mycdev001 c  403 0

/**
 * make
 * make scp
 *
 * test ok on tinkerboard.
 *
 * need to get major number from /proc/devices
 *
 * sudo mknod /dev/mydev1012 c 238 0
 *
 *
 *
 *

 sudo cat /proc/iomem

 ff720000-ff7200ff : /pinctrl/gpio0@ff720000
 ff730000-ff7300ff : /pinctrl/gpio1@ff730000
 ff780000-ff7800ff : /pinctrl/gpio2@ff780000
 ff788000-ff7880ff : /pinctrl/gpio3@ff788000
 ff790000-ff7900ff : /pinctrl/gpio4@ff790000



 RK3399 has 5 groups of GPIO banks: GPIO0~GPIO4, and each group is distinguished by numbers A0~A7, B0~B7, C0~C7, D0~D7.

 地址：
 GPIO0 ： FF72_0000
 GPIO1 ： FF73_0000
 GPIO2 ： FF78_0000
 GPIO3 ： FF78_8000
 GPIO4 ： FF79_0000


 Offset
 GPIO_SWPORTA_DR  : 0x0000
 GPIO_SWPORTA_DDR : 0x0004

 GPIO_1_A7

 *
 *
 *
 */

#define MY_MAJOR       499
//#define GPIO0_BASE  0xFF720000
//#define GPIO0_BASE  0xFF730000

#define GPIO_1_BASE  0xFF730000
#define  GPIO1_SWPORTA_DR GPIO_1_BASE

#define  GPIO1_SWPORTA_DDR  0xFF730004

#define LOCATION  7

#define GPIO_1_A7  0x1

//#define  DIRECTION_OUT 255
//
//#define  LED1_ON    255

#define MY_MAX_MINORS  2

#define   DEVICE_NAME  "device name 499"

struct my_device_data {
	struct cdev cdev;
	char *desc;
};

/**
 *  cat /sys/module/a2/parameters/name
 */
static char *name = "linux内核模块入门之 Hello World";

module_param(name, charp, S_IRUGO);

struct my_device_data devs[MY_MAX_MINORS];

//static void on(void) {
//	writel(readl(direction) | (0x1 << 8), direction);
//	writel(readl(data) | (0x1 << 8), data);
//}

static int my_open(struct inode *inode, struct file *file) {
	unsigned int __iomem *data = ioremap(GPIO1_SWPORTA_DR, 4);
	unsigned int __iomem *direction = ioremap(GPIO1_SWPORTA_DDR, 4);
	unsigned int a = readl(direction);
	unsigned int d = readl(data);
	pr_err("direction: %d \n",a);
	pr_err("direction 1 or 0  ? : %d \n",(a & (GPIO_1_A7 << LOCATION)) >> LOCATION);
	pr_err(" data %d \n",d);
	pr_err(" data 1 or 0 ? : %d \n",(d & (GPIO_1_A7 << LOCATION)) >> LOCATION);
//	on();
	pr_info("a3 my_open  1330 \n");


	writel(readl(direction) | (GPIO_1_A7 << LOCATION), direction);
    writel(readl(data) | (GPIO_1_A7 << LOCATION), data);


//	switch(MINOR(inode->i_cdev)){
//	default :
//		file->private_data =
//
//	}
	file->private_data = container_of(inode->i_cdev, struct my_device_data,
			cdev);
	((struct my_device_data*) file->private_data)->desc = "my dec 789";
//	pr_info("%s\n",((struct my_device_data*)file->private_data)->desc);
	return 0;
}

static unsigned char mem[100];

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	size_t my_size = 100;
	ssize_t len = min(my_size - (ssize_t )*offset, size);
	pr_info("a3 my_write\n");

	pr_info("write");
	if (len <= 0)
		return 0;
//
	/* read data from user buffer to my_data->buffer */
	if (copy_from_user(mem + *offset, user_buffer, len))
		return -EFAULT;
	*offset += len;
	pr_info("r: %s", mem);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *buffer, size_t count,
		loff_t *offset) {

	int p = *offset;
	uint8_t *data = "haha,1012\n";
	size_t datalen = strlen(data);
	struct my_device_data *device_data = filp->private_data;

	pr_info("desc: %s\n", device_data->desc);

//	pr_info("a3 my_read: %d \n",r++);
//		pr_info("length of the buffer : %ul \n",count);
//		pr_info("offset : %ul \n",*offset);

	if (p > datalen) {
		return 0;
	}
	if (count > datalen - p) {
		count = datalen - p;
	}
	if (copy_to_user(buffer, data + *offset, count)) {
		return -EFAULT;
	}
	*offset += count;
	return count;
}

int my_close(struct inode *inode, struct file *file) {
	pr_info("a3 my_close\n");
	return 0;
}

const struct file_operations my_fops = { .owner = THIS_MODULE, .open = my_open,
		.read = my_read, .write = my_write, .release = my_close,
//		.unlocked_ioctl = my_ioctl
		};

static dev_t mydev;

static __init int my_init(void) {
	int err;
	int i;

	pr_info("a3 init_module\n");
	pr_info("name is %s\n", name);;

//	err = register_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS,
//			DEVICE_NAME);

	err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i), 1);
	}

	return 0;
}

static void __exit my_exit(void) {
	int i;
	pr_info("a3 cleanup_module\n");
	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
	}

	unregister_chrdev_region(mydev, MY_MAX_MINORS);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("A sample c  driver");
